package com.learning.jquizenglish.screen.learningfeed;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.activity.MainActivity;
import com.learning.jquizenglish.common.fragment.JQuizEnglishFragment;
import com.learning.jquizenglish.model.JQuizRequestModel;
import com.learning.jquizenglish.model.QuestionModel;
import com.learning.jquizenglish.model.ResultModel;
import com.learning.jquizenglish.network.JQuizEnglishApi;
import com.learning.jquizenglish.utility.DialogUtil;
import com.learning.jquizenglish.utility.DialogUtil.OnDialogListener;
import com.learning.jquizenglish.widget.smoothprogressbar.SmoothProgressBar;

public class LearningFeedScreen extends JQuizEnglishFragment{

	public ExpandableListView mListView;
	private LearningFeedExpandAdapter mAdapter;
	private SmoothProgressBar mSmoothProgressBar;
	private ArrayList<QuestionModel> mListDatas;
	private Dialog dialogPlayAudio;
	private Button btnPlayStop;
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.screen_learning_feed, container, false);
		mListView = (ExpandableListView) v.findViewById(R.id.lvResult);
		mSmoothProgressBar = (SmoothProgressBar) v.findViewById(R.id.progressbar);
		initAudioPlayDialog();
		initData();
		return v;
	}
	
	private void initData(){
		JQuizRequestModel requestModel = JQuizEnglishApi.getRequestLearningFeed();
		executeGetTaskReturnList(requestModel, false, QuestionModel.class, new JQuizTaskListener(){

			@Override
			public void onTaskComplete(Object object) {
				if(object!=null){
					mListDatas = (ArrayList<QuestionModel>) object;
					mAdapter = new LearningFeedExpandAdapter(LearningFeedScreen.this,getActivity(), mListDatas);
					mListView.setAdapter(mAdapter);
					mListView.setOnGroupClickListener(new OnGroupClickListener() {
						
						@Override
						public boolean onGroupClick(ExpandableListView parent, View v,
								final int groupPosition, long id) {
							QuestionModel model = (QuestionModel) mAdapter.getGroup(groupPosition);
							if (model!=null && model.isWaitingHide) {
								DialogUtil.showWhyDontShowDialog(getActivity(), new OnDialogListener() {
									
									@Override
									public void onPositive(String result) {
										mAdapter.hideGroup(groupPosition);
									}
									
									@Override
									public void onCancel() {
										
									}
								});
							}
							return true;
						}
					});
//					int size = mListDatas.size();
//					for (int i = 0; i < size; i++) {
//						mListView.expandGroup(i);
//					}
					mListView.setGroupIndicator(null);
				}
				mSmoothProgressBar.setVisibility(View.GONE);
			}
			
		});
	}
	
	private QuestionModel currentQuestionAudio = null;
	public void onShowDialogPlayAudio(QuestionModel model){
		dialogPlayAudio.show();
		((MainActivity)getActivity()).setAudioUrl(JQuizEnglishApi.BASE_AUDIO_URL+model.audio);
	}
	
	private void initAudioPlayDialog(){
		dialogPlayAudio = new Dialog(getActivity());
		dialogPlayAudio.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_play_audio, null);
		btnPlayStop = (Button) vContent.findViewById(R.id.btnPlayStop);
		((MainActivity)getActivity()).setButtonController(btnPlayStop);
		dialogPlayAudio.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss(DialogInterface dialog) {
				((MainActivity)getActivity()).stopAudio();
			}
		});
		int dialogWidth = MainActivity.SCREEN_WIDTH*3/4;
		dialogPlayAudio.setContentView(vContent,new LayoutParams(dialogWidth, LayoutParams.WRAP_CONTENT));
	}
	
	public void onComment(final QuestionModel model) {
		DialogUtil.showCommentDialog(getActivity(), model, null);
	}
	
	public void onHelp(final QuestionModel model) {
		DialogUtil.showHelpDialog(getActivity(), model, null);
	}
	
	public void onMore(final int groupPosition){
		DialogUtil.showMoreDialog(getActivity(), new OnDialogListener() {
			
			@Override
			public void onPositive(String result) {
				mAdapter.dontShowGroup(groupPosition);
			}
			
			@Override
			public void onCancel() {
				
			}
		});
	}
}
